﻿using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.DependencyInjection;

namespace ApiTemplate.XUnit
{
    /// <summary>
    /// 初始化单元测试
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            string basePath = ApplicationEnvironment.ApplicationBasePath;

            string appFile = Path.Combine(basePath, "ApiTemplate.Application.dll");
            var assemblysApp = System.Reflection.Assembly.LoadFrom(appFile);
            var types = assemblysApp.GetTypes();

            string appInterfaceFile = Path.Combine(basePath, "ApiTemplate.IApplication.dll");
            var assemblysAppInterface = System.Reflection.Assembly.LoadFrom(appInterfaceFile);
            var typeInterfaces = assemblysAppInterface.GetTypes();
            foreach (Type type in types)
            {
                Type? interFace = typeInterfaces.FirstOrDefault(x => x.Name == "I" + type.Name);
                if (interFace != null)
                {
                    services.AddScoped(interFace, type);
                }
            }
        }
    }
}
